﻿using System;
using System.Collections.Generic;
using Sharp.DataTransferObject.CMS;
using Sharp.Domain.CMSBoundedContext.MemberModule.Aggregates;
using Sharp.Domain.CMSBoundedContext.OperationModule.Aggregates;
using Sharp.Domain.Repositories;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Search;
using Sharp.ServiceContracts.CMS;

namespace Sharp.Application.CMS
{
    public partial class MemberServiceImpl : ApplicationService, IMemberService
    {
        private readonly INoiseWordRepository _noiseWordRepository;
        private readonly IIPBlackListRepository _ipBlackListRepository;
        private readonly IOAuthRepository _oAuthRepository;
        private readonly ICommentRepository _commentRepository;
        private readonly IFavoriteRepository _favoriteRepository;
        public MemberServiceImpl(IRepositoryContext context,
            INoiseWordRepository noiseWordRepository,
             IIPBlackListRepository ipBlackListRepository,
             IOAuthRepository oAuthRepository,
             ICommentRepository commentRepository,
             IFavoriteRepository favoriteRepository
            ) : base(context)
        {
            _noiseWordRepository = noiseWordRepository;
            _ipBlackListRepository = ipBlackListRepository;
            _oAuthRepository = oAuthRepository;
            _commentRepository = commentRepository;
            _favoriteRepository = favoriteRepository;
        }

        #region Oauth
        public Result<IEnumerable<OAuthDTO>> GetOauthList()
        {
            var res = new Result<IEnumerable<OAuthDTO>>
            {
                Data = _oAuthRepository.GetList().To<OAuth, OAuthDTO>()
            };
            return res;
        }

        public Result SaveOauth(OAuthDTO dtoModel)
        {
            Result result = new Result();
            if (dtoModel.Id.IsEmpty())
            {
                OAuth oAuth = dtoModel.To<OAuth>();
                _oAuthRepository.Add(oAuth);
                _oAuthRepository.Context.Commit();
                return result;
            }
            var persisted = _oAuthRepository.Get(dtoModel.Id);
            persisted.Interface = (OAuth.OAuthType)dtoModel.Interface;
            persisted.AppId = dtoModel.AppId;
            persisted.AppKey = dtoModel.AppKey;
            persisted.IsEnable = dtoModel.IsEnable;
            _oAuthRepository.Update(persisted);
            _oAuthRepository.Context.Commit();
            return result;
        }

        public PagedResult<FavoriteDTO> GetFavoritePaged(PagingFeature pf)
        {
            var paged = _favoriteRepository.Pagging(pf, null, t => t.VIU);

            return paged.To<Favorite, FavoriteDTO>();
        }

        public Result DeleteFavorites(Guid[] ids)
        {
            var res = new Result();
            if (ids == null)
            {
                res.Err = "参数不可为空。";
                return res;
            }

            _favoriteRepository.Remove(ids);

            Context.Commit();

            return res;
        }

        #endregion

    }
}
